<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<meta name="HandheldFriendly" content="true" />
		<meta name="MobileOptimized" content="320" />
		<title>Hello H5+</title>
		<link rel="stylesheet" href="./css/common.css" type="text/css" charset="utf-8" />
		<script type="text/javascript" src="./js/common.js"></script>
		<script src="js/mui.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/urlfun.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/publicErr.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/request.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			mui.init();
			var ws = null,
				wo = null;
			var scan = null,
				domready = false,
				bCancel = false;
			var resultl,type,file ='';
			var did = localStorage.getItem("did");
			// H5 plus事件处理
			function plusReady() {
				if(ws || !window.plus || !domready) {
					return;
				}
				// 获取窗口对象
				ws = plus.webview.currentWebview();
				wo = ws.opener();
				// 开始扫描
				ws.addEventListener('show', function() {
					scan = new plus.barcode.Barcode('bcid', [plus.barcode.QR, plus.barcode.EAN8, plus.barcode.EAN13], {
						frameColor: '#00FF00',
						scanbarColor: '#00FF00'
					});
					scan.onmarked = onmarked;
					scan.start({
						conserve: true,
						filename: '_doc/barcode/'
					});
				});
				did = plus.device.uuid.toString();
				// 显示页面并关闭等待框
				ws.show('pop-in');
				wo.evalJS('closeWaiting()');
			}
			if(window.plus) {
				plusReady();
			} else {
				document.addEventListener('plusready', plusReady, false);
			}
			// 监听DOMContentLoaded事件
			document.addEventListener('DOMContentLoaded', function() {
				domready = true;
				plusReady();
			}, false);
			// 二维码扫描成功
			function onmarked(type, result, file) {
				switch(type) {
					case plus.barcode.QR:
						type = 'QR';
						break;
					case plus.barcode.EAN13:
						type = 'EAN13';
						break;
					case plus.barcode.EAN8:
						type = 'EAN8';
						break;
					default:
						type = '其它' + type;
						break;
				}
				result = result.replace(/\n/g, '');
				console.log(result);
					var data = {
						code:result,
					}
					request.requestAjax("post","card/get_code",data,function(res){
						console.log(JSON.stringify(res));
//						alert(JSON.stringify(res));
						if(res.state == true){
							if(res.data.can_consume){
								var btnArray = ['是', '否'];
								mui.confirm('消除此 "' + res.data.card.title + '" 券？', '提示', btnArray, function(e) {
				                    if (e.index == 1) {
				                    	plus.nativeUI.toast('取消核销！');
				                      mui.openWindow({
											url: "auth.html",
											id: "auth.html",
											createNew:true,
										})
				                    } else {
				                    	comsume(result);
				                      
				                    }
				                })
								
							}else{
								plus.nativeUI.toast("找不到此卡券！");
								mui.openWindow({
											url: "auth.html",
											id: "auth.html",
											createNew:true,
								})
							}
						}
						if(res.code){
							pushAuth();
						}
					},function(res){
						pushAuth();
						console.log(JSON.stringify(res))
						console.log("网络错误")
					})
				
			}
			function comsume(result){
				var data = {
						code:result,
					}
					request.requestAjax("post","card/comsume",data,function(res){
						console.log(JSON.stringify(res))
//						alert(JSON.stringify(res));
						if(res.state == true){
							if(res.data.errcode == 0){
								console.log("核销成功！");
								plus.nativeUI.toast('核销成功！');
								mui.openWindow({
									url: "auth.html",
									id: "auth.html",
									createNew:true,
								})
							}else{
								plus.nativeUI.toast("核销失败！");
								mui.openWindow({
											url: "auth.html",
											id: "auth.html",
											createNew:true,
								})
								console.log("核销失败！")
							}
						}
						if(res.code){
							pushAuth();
						}
					},function(res){
						pushAuth();
						console.log(JSON.stringify(res))
						console.log("网络错误")
					})
			}
		</script>

		<style type="text/css">
			#bcid {
				width: 100%;
				position: absolute;
				top: 0px;
				bottom: 44px;
				text-align: center;
			}
			
			.tip {
				color: #FFFFFF;
				font-weight: bold;
				text-shadow: 0px -1px #103E5C;
			}
			
			footer {
				width: 100%;
				height: 44px;
				position: absolute;
				bottom: 0px;
				line-height: 44px;
				text-align: center;
				color: #FFF;
				background-color: #01AEDB;
			}
			
			.fbt {
				width: 100%;
				height: 100%;
				background-color: #01AEDB;
				float: left;
			}
			
			.fbt:active {
				-webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.5);
				box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.5);
			}
		</style>
	</head>

	<body style="background-color: #000000;">
		<div id="bcid">
			<div style="height:40%"></div>
			<p class="tip">...载入中...</p>
		</div>
		<footer>
			<div class="fbt" onclick="back()">取　消</div>
			<!--<div id="btCancel" class="fbt" onclick="scanSwitch()">重　扫</div>-->
		</footer>
	</body>

</html>